/*
 * case_test.cpp
 *
 *  Created on: 11-09-2012
 *      Author: mith
 */

#include "case_test.h"
// MY HEADERS
#include"mros-v42.h"

namespace case_test
{



int function(const gsl_vector * x, void *params, gsl_vector * f) {

    double a = ((Par *) params)->get_a();
    double b = ((Par *) params)->get_b();

	if(DEEP_DEBUG) {
		printf("### %25.15e %25.15e\n", gsl_vector_get (x, 0), gsl_vector_get (x, 1));
	}

    const double x0 = gsl_vector_get (x, 0);
    const double x1 = gsl_vector_get (x, 1);

    const double y0 = a * (1 - x0);
    const double y1 = b * (x1 - x0 * x0);

    gsl_vector_set (f, 0, y0);
    gsl_vector_set (f, 1, y1);

	if(DEEP_DEBUG) {
		printf("### %25.15e %25.15e\n", gsl_vector_get (x, 0), gsl_vector_get (x, 1));
		printf("###  %24.15e# %24.15e#\n", gsl_vector_get (f, 0), gsl_vector_get (f, 1));
		printf("\n");
	}

    return GSL_SUCCESS;
}

// ************* CLASSES ************* //

// Par

void Par::print(FILE *file) const {
	fprintf(file, "%10.6f %10.6f", get_a(), get_b());
}

void Par::print_verboten(FILE *file) const {
	fprintf(file, "PARAMETERS: a=%f, b=%f\n", get_a(), get_b());
}

void Par::print_label(FILE *file) const {
	fprintf(file, "     a               b");
}

// Var

void Var::print(FILE *file) const {
	if(NtabD <= 0) { exit(EXIT_FAILURE); }
	for(int i=0; i<NtabD-1; i++) { fprintf(file, "%10.6e", tabD[i]); }
	fprintf(file, " %10.6e", tabD[NtabD-1]);
}

void Var::print_verboten(FILE *file) const{
	fprintf(file, "VARIABLES:");
	for(int i=0; i<NtabD; i++) { fprintf(file, " %10.6e", tabD[i]); }
	fprintf(file, "\n");
}

void Var::print_label(FILE *file) const {
	printf("       x0                 x1");
}

// Res

void Res::set(gsl_multiroot_fsolver *s) {
	for(int i=0; i<n; i++) {
		(*V)[i] = gsl_vector_get (s->x, i);
	}
}

void Res::print(FILE *file) const { V->print(file); }
void Res::print_verboten(FILE *file) const { fprintf(file, "Result: "); V->print_verboten(file); }
void Res::print_label(FILE *file) const { V->print_label(file); }

void example() {
	printf("Test\n");
    Par par_t(1, 10);
    Var var_t(N_VAR); var_t.set_x0(-10); var_t.set_x1(-5);
    printf("Starting point: "); var_t.print(); printf("\n");
    Res res_t(N_VAR);
    RootGSL root_t(par_t, function, res_t);
    root_t.Solve(var_t);
    root_t.print_verboten();
}

} // end of namespace case_test
